//
//  LoginEngine.m
//  HuLaQuan
//
//  Created by hok on 1/13/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "LoginEngine.h"

#import "CurrentUser.h"

@implementation LoginEngine

- (void)loginWithUsername:(NSString *)name andPassword:(NSString *)password
{
    
    
    NSString *pw = [RSA encryptString:password publicKey:PUBLIC_KEY];

    NSString *randomKey = [self createKeyWithLength:24];

  // NSString *loginSrc = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"userLogin\" sysName=\"usapp\" sign=\"\" serialNumber=\"201511041439000012\" userId=\"\" termId=\"64F43435-2C0F-47C8-B0F7-91B20F5261F7\" termAdd=\"106.63,26.65\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.4\" tokenValue=\"899a6a958607eff2eae96c0813580859\" mchntId=\"898520000066666\"><loginName>18685006543</loginName><loginPwd>YUhidWG1UQCyeYnQ7AIqHIj/pikh6jq3k/IL/L/ZR+W58Enm6T4crg7gQD1hZKUD3uG0ZmMZq/a0+kOHsHe8bJyKJfX1IwqO35Lbt6Qim+Laxl9SQeNyp+VZ8IeKusPDaA9sWUDdNpnzfvXAvDzNmcLsN3S3Xfz6t7n+0WBNhNo=</loginPwd></msg>";
    NSString *loginSrc = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"userLogin\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"\" userId=\"\" channelId=\"88888888\"><loginName>%@</loginName><loginPwd>%@</loginPwd><attach>attach</attach></msg>",[self getSerialNumber],name,pw];
//
    
    
    NSLog(@"%@ -- loginsrc ---%@ key ",loginSrc,randomKey);

    
    NSString *first =[self firstString];

    NSString *second = [self secondStringWithKey:randomKey];
    NSString *third = [self thirdStringWithBody:loginSrc andKey:randomKey];
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",first,second,third];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            if ([item isKindOfClass:[NSArray class]]) {
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                
                NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
                NSLog(@"%@ %@",resString ,resultDic);
                
                NSMutableDictionary *userInfoDic =[[NSMutableDictionary alloc] init];
                [userInfoDic setObject:password forKey:@"password"];
                [userInfoDic setObject:name forKey:@"userName"];
                [userInfoDic setObject:resultDic[@"tokenValue"] forKey:@"tokenValue"];
                [userInfoDic setObject:resultDic[@"userId"] forKey:@"userId"];
                
                [[CurrentUser sharedInstance] createUserWithUserInfoDictionary:userInfoDic];
                
                result = @{@"isSuccess":@YES};
                
                NSLog(@"%@",[CurrentUser sharedInstance].description);
            }
            
        }else{
            
            result = @{@"isSuccess":@NO,@"message":item};
            NSLog(@"%@",result);
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginFinish" object:result];
    }];
}
@end
